package com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad

import com.anythink.interstitial.api.ATInterstitial
import com.bp.hearthealth.pulse.lnapps.BpApplication
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.listener.ad_action.TopOnOpenAdListener
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.observer.SplashType
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.observer.TopOnSplashFinishObservable
import com.bp.hearthealth.pulse.lnapps.appsflyer.AppsFlyerUtils
import com.bp.hearthealth.pulse.lnapps.base.BpBaseActivity
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants
import com.bp.hearthealth.pulse.lnapps.bp_utils.SpUtils
import com.bp.hearthealth.pulse.lnapps.databinding.SplashAdShowBinding

class SplashAdShowActivity : BpBaseActivity<SplashAdShowBinding>() {
    private var firstShow: String? = null
    override fun initListener() {
        super.initListener()
        val isColdLaunch = intent.getIntExtra("isColdLaunch", 0)
        firstShow = SpUtils.getString(Constants.IS_FIRST_SHOW)
        LogUtil.iTopOn("TopOn 【开屏广告界面】")
        TopOnOpenAdManager.setExListener(object : TopOnOpenAdListener {
            override fun onAdDismiss() {
                LogUtil.iTopOn("TopOn 【SplashAdShowActivity】 onAdDismiss")
                BpApplication.adIsShow = false
                TopOnSplashFinishObservable.notifySplash(SplashType.TYPE_SPLASH_FINISH)
                finish()
            }

            override fun onAdShow() {
                //isColdLaunch: 0:前后台切换,1:冷启动,2:热启动
                when (isColdLaunch) {
                    0 -> {
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_OPEN_ID, "f66471e7bA77b00")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【前后台切换后_开屏广告】")
                    }

                    1 -> {
                        if (!SpUtils.getBoolean("app_first_open")) {
                            //第一次展示  app_first_open
                            AppsFlyerUtils.trackEvent("app_first_open")
                            //第一次冷启动开屏
                            LogUtil.iTopOn("TopOn 【开屏广告】 第一次冷启动开屏显示 f66471e28ce125")
                            ATInterstitial.entryAdScenario(
                                Constants.TOP_ON_OPEN_ID,
                                "f66471e28ce125"
                            )
                            LogUtil.iTopOn("TopOn 配置的场景ID为: 【第一次冷启动后_开屏广告】")
                            SpUtils.saveBoolean("app_first_open", true)
                        } else {
                            //非第一次冷启动开屏
                            LogUtil.iTopOn("TopOn 【开屏广告】 非第一次冷启动开屏显示 f66471e50cfaa0")
                            ATInterstitial.entryAdScenario(
                                Constants.TOP_ON_OPEN_ID,
                                "f66471e50cfaa0"
                            )
                            LogUtil.iTopOn("TopOn 配置的场景ID为: 【非第一次冷启动后_开屏广告】")
                        }
                    }

                    else -> {
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_OPEN_ID, "f66471e661ecd0")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【热启动后_开屏广告】")
                    }
                }
                AppsFlyerUtils.trackEvent("ad_open_launch_show")
                if (firstShow.isNullOrEmpty()) {
                    LogUtil.iTopOn("启动页第一次展示-开屏广告展示 事件埋点")
                    //启动页第一次展示-开屏广告展示  ad_open_first_open_show
                    AppsFlyerUtils.trackEvent("ad_open_first_open_show")
                    SpUtils.saveString(Constants.IS_FIRST_SHOW, "set")
                }
                BpApplication.adIsShow = true
            }

            override fun onNoAdError() {
                TopOnSplashFinishObservable.notifySplash(SplashType.TYPE_SPLASH_FINISH)
                finish()
                LogUtil.iTopOn("TopOn 【SplashAdShowActivity】 onNoAdError")
            }
        })
        LogUtil.i("TopOnOpenAdManager.mSplashAd = ${TopOnOpenAdManager.mSplashAd}")
        if (null != activity && TopOnOpenAdManager.mSplashAd != null) {
            TopOnOpenAdManager.mSplashAd!!.show(
                activity,
                binding.splashAdContainer
            )
        } else {
            finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        TopOnOpenAdManager.releaseListener()
        LogUtil.iTopOn("TopOn 销毁【开屏广告界面】")
    }
}
